$(function () {
    var emp_datagrid = $("#emp_datagrid");
    var emp_dialog = $("#emp_dialog");
    var emp_form = $("#emp_form");

    var cmdObj = {
        //工具栏的刷新
        reload: function () {
            emp_datagrid.datagrid("reload");
        },
        //导出Excel
        export: function () {
            var columnField = emp_datagrid.datagrid('getColumnFields');
            var table = [];
            for (var i = 0; i < columnField.length; i++) {
                var tableName = emp_datagrid.datagrid('getColumnOption', columnField[i]).title;
                table.push(encodeURIComponent(encodeURIComponent(tableName)));
            }
            window.location.href = "/employee/export?" + 'titles=' + table;
        },

        //工具栏添加
        add: function () {
            //添加与编辑时
            emp_form.form("clear");
            //1.弹框
            emp_dialog.dialog("open");
            //设置标题
            emp_dialog.dialog("setTitle", "员工添加");
        },
        //编辑
        edit: function () {
            //判断是否选中数据
            var row = emp_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选择要编辑的数据");
                return;
            }
            //添加与编辑时将数据清空
            emp_form.form("clear");
            //部门名字不一样, 需要修改
            if (row.dept) {
                row["dept.id"] = row.dept.id;
            }
            //角色组件name特殊, 无法使用同名原则自动赋值, 需要手动赋值
            $.get("/role/getRoleIdByEmpId?empId=" + row.id, function (data) {
                $("#rolesId").combobox("setValues", data);
            });
            //数据回显: 同名原则
            emp_form.form("load", row);
            //1.弹框
            emp_dialog.dialog("open");
            //设置标题
            emp_dialog.dialog("setTitle", "员工编辑");
        },

        //工具栏离职/复制
        changeState: function () {
            //判断是否选中数据
            var row = emp_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选择要编辑的数据");
                return;
            }
            $.messager.confirm('确认', '您确认想要执行这个操作吗？', function (r) {
                if (r) {
                    $.get("/employee/changeState", {id: row.id}, function (data) {
                        if (data.success) {
                            //刷新列表数据
                            emp_datagrid.datagrid("reload");
                        } else {
                            $.messager.alert('温馨提示', data.msg);
                        }
                    })
                }
            });
        },
        //保存操作
        save: function () {
            var id = $("#empId").val();
            var url = "/employee/save";
            if (id) {
                url = "/employee/update";
            }
            emp_form.form('submit', {
                url: url,
                onSubmit: function (param) {
                    //格式roles[0].id
                    var ids = $("#rolesId").combobox("getValues");
                    for (var i = 0; i < ids.length; i++) {
                        param["roles[" + i + "].id"] = ids[i];
                    }
                },
                success: function (data) {
                    data = $.parseJSON(data);

                    if (!data.success) {
                        $.messager.alert('温馨提示', data.msg);
                    } else {
                        //关闭框
                        emp_dialog.dialog("close");
                        //刷新列表数据
                        emp_datagrid.datagrid("reload");
                    }
                }
            })
        },
        //关闭
        cancel: function () {
            emp_dialog.dialog("close");
        }

    }

    //调用
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });

    emp_datagrid.datagrid({
        url: "employee/list",
        fit: true,
        fitColumns: true,
        striped: true,
        singleSelect: true,
        pagination: true,
        rownumbers: true,
        columns: [[
            {field: "id", title:"id",hidden: true},
            {field: "username", title: "名称", width: 100},
            {field: "realname", title: "真实名", width: 100},
            {field: "tel", title: "电话", width: 100},
            {field: "email", title: "邮箱", width: 100},
            {
                field: "dept", title: "部门", width: 100, formatter: function (value, row, index) {
                if (value) {
                    return value.name;
                } else {
                    return "未分配";
                }
            }
            },
            {field: "hiredate", title: "入职日期", width: 100, sortable: true, order: "desc"},
            {
                field: "admin", title: "是否是超级管理员", width: 100, formatter: function (value, row, index) {
                //console.log(value);
                return value ? "是" : "否";
            }
            },
            {
                field: "state", title: "状态", width: 100, formatter: function (value, row, index) {
                //console.log(value);
                if (value == 1) {
                    return "<font color='green'>在职</font>";
                } else if (value == 0) {
                    return "<font color='red'>离职</font>";
                }
            }
            },
            {field: "ck",checkbox: true}//显示多选框样式

        ]],
        toolbar: '#tb',
        onClickRow: function (index, row) {
            //根据员工的状态修改离职/复职按钮的显示
            if (row.state == 1) {
                $("#change_btn").linkbutton({
                    text: "离职"
                });
            } else {
                $("#change_btn").linkbutton({
                    text: "在职"
                })
            }
        }

    });
    emp_dialog.dialog({
        width: 400,
        height: 420,
        buttons: "#bb",
        closed: true
    })


});


//工具栏的高级查询
function query() {
    var kw = $("#keyword").textbox("getValue");
    var startDate = $("input[name='startDate']").val();
    var endDate = $("input[name='endDate']").val();
    var deptId = $("input[name='deptId']").val();
    $("#emp_datagrid").datagrid("load", {
        keyword: kw,
        startDate: startDate,
        endDate: endDate,
        deptId: deptId
    });
}
